package com.sixmac.controller.backend;

import com.sixmac.common.DataTableFactory;
import com.sixmac.controller.common.CommonController;
import com.sixmac.entity.Area;
import com.sixmac.entity.Outlet;
import com.sixmac.service.OperaService;
import com.sixmac.service.OutletService;
import com.sixmac.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/30 0030.
 */
@Controller
@RequestMapping(value = "backend/outlet")
public class OutletController extends CommonController {

    @Autowired
    private OutletService outletService;

    @Autowired
    private OperaService operaService;

    @RequestMapping("/index")
    public String index() {
        return "backend/营业网点列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);

        Page<Outlet> page = outletService.find(pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/show")
    public String info(ModelMap model, Integer id, String type) {
        if (null != id) {
            Outlet outlet = outletService.getById(id);
            model.addAttribute("outlet", outlet);
            model.addAttribute("areaId", outlet.getArea().getId());
        }

        if (StringUtils.isNotEmpty(type)) {
            return "backend/编辑营业网点";
        } else {
            return "backend/查看营业网点";
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public Integer save(HttpServletRequest request, Integer id, String name, Integer areaId, String address, String phone, String description) {
        try {
            Outlet outlet = null;

            if (null == id) {
                outlet = new Outlet();
            } else {
                outlet = outletService.getById(id);
            }

            outlet.setName(name);
            Area area = new Area();
            area.setId(areaId);
            outlet.setArea(area);
            outlet.setAddress(address);
            outlet.setPhone(phone);
            outlet.setDescription(description);
            outlet.setUpdateTime(new Date());

            outletService.update(outlet);

            operaService.addOperaInfo(request, (null == id ? "添加" : "修改") + "营业网点 " + outlet.getName());

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(HttpServletRequest request, Integer id) {
        try {
            Outlet outlet = outletService.getById(id);

            operaService.addOperaInfo(request, "删除营业网点 " + outlet.getName());

            outletService.deleteById(id);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
